<template>
  <div class="login_container">
    <div class="login_box juzhong">
      <div class="avatar_box">
        <img src="../../assets/logo.png" alt="">
      </div>
      <div class="login_form">
        <el-form :model="loginFrom" status-icon ref="loginFrom"  label-width="100px" class="demo-ruleForm" :rules="rules">
          <el-form-item label="用户名" prop="username">
            <el-input  prefix-icon="el-icon-user" v-model="loginFrom.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input  prefix-icon="el-icon-lock" type="password" autocomplete="off" v-model="loginFrom.password"></el-input>
          </el-form-item>
          <el-form-item class="login_btn">
            <el-button type="primary" @click="submitForm('loginFrom')">提交</el-button>
            <el-button @click="resetLoginForm('loginFrom')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.loginFrom.password !== '') {
        }
        callback()
      }
    }
    return {
      loginFrom: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm (loginFrom) {
      this.$refs.loginFrom.resetFields()
    },
    submitForm (loginFrom) {
      this.$refs.loginFrom.validate(async valid => {
        if (valid) {
          const { data } = await this.$http.post('login', this.loginFrom)
          if (data.meta.status === 200) {
            this.open()
            window.sessionStorage.setItem('token', data.data.token)
            this.$router.push('/home')
            console.log(data.data.token)
          }
        }
      })
    },
    open () {
      this.$message({
        message: '登陆成功',
        type: 'success'
      })
    }
  }
}
</script>

<style lang='less' scoped>
  .login_container{
    background-color: #2b4b6b;
    height: 100%;
    .login_box{
      width: 450px;
      height: 300px;
      background-color: #fff;
      border-radius: 3px;
      .avatar_box{
        height: 100px;
        width: 100px;
        border: 1px solid #eee;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 10px #ddd;
        position: absolute;
        margin: auto;
        left: 0;
        right: 0;
        top: -20%;
        background-color: #fff;
        img{
          width: 100%;
          height: 100%;
          border-radius: 50%;
          background-color: #eee;
        }
      }
      .login_btn{
        display: flex;
        justify-content: flex-end;
      }
      .login_form{
        position: absolute;
        bottom: 20px;
        width: 85%;
      }
    }
  }
</style>
